package rensoft.portfolio.client.model.titles;

import com.google.gwt.core.client.GWT;

import java.util.Iterator;

import cs.java.collections.LinkedHashMap;
import cs.java.collections.LinkedMap;
import cs.java.lang.HasService;


import rensoft.gwt.client.lang.GwtBase;
import rensoft.portfolio.client.model.titles.Titles.TitleState;
import rensoft.portfolio.client.resources.pages.PagesData;
import rensoft.portfolio.client.resources.pages.WorksData;

public abstract class TitleImpl extends GwtBase implements Title {

  protected static final int HEIGHT = 390;

  protected static final PagesData pagesData = GWT.create(PagesData.class);
  protected static final WorksData worksData = GWT.create(WorksData.class);
  private int currentIndex = 0;
  private final LinkedMap<String, Page> pages = new LinkedHashMap<String, Page>();
  private final TitleState type;

  public TitleImpl(HasService parent, TitleState type) {
    this.type = type;
    initialize(parent);
  }

  public int getCurrentIndex() {
    return currentIndex;
  }

  @Override
  public int getIndex(Page page) {
    return pages.indexOf(page.getId());
  }

  public Page getPage() {
    return getPage(currentIndex);
  }

  public Page getPage(int index) {
    return pages.getValue(index);
  }

  public int getPageCount() {
    return pages.size();
  }

  public TitleState getType() {
    return type;
  }

  public Iterator<Page> iterator() {
    return pages.values().iterator();
  }

  public void showPage(int index) {
    if (currentIndex == index) return;
    currentIndex = index;
    fireEvent(ON_PAGE_CHANGE, getPage());
  }

  public void showPage(String key) {
    showPage(pages.indexOf(key));
  }

  protected void add(Page page) {
    pages.put(page.getId(), page);
  }
}
